clear
set more off

cap log close

***** 30-year*****

use $rawdir/1880_1%.dta, clear

	set seed 828
	gen randomorder = runiform()
	
	gen region_birth = 11 if bpl==9 | bpl==23 | bpl==25 | bpl==33 | bpl==44 | bpl==50
	replace region_birth = 12 if bpl==36 | bpl==34 | bpl==42
	replace region_birth = 21 if bpl==17 | bpl==18 | bpl==26 | bpl==39 | bpl==55
	replace region_birth = 22 if bpl==19 | bpl==20 | bpl==27 | bpl==29 | bpl==31 | bpl==38 | bpl==46
	replace region_birth = 31 if bpl== 10 | bpl==11 | bpl==24 | bpl==12 | bpl==13 | bpl==37 | bpl==45 | bpl==51 | bpl==54
	replace region_birth = 32 if bpl==1 | bpl==21 | bpl==28 | bpl==47
	replace region_birth = 33 if bpl==5 | bpl==22 | bpl==40 | bpl==48
	replace region_birth = 41 if bpl==4 | bpl==8 | bpl==16 | bpl==32 | bpl==30 | bpl==35 | bpl==56 | bpl==49
	replace region_birth = 42 if bpl==6 | bpl==41 | bpl==53
	
	gen region_birth_coarse = floor(region_birth/10)
	replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
	
	*** NAMES
	*** first name 
	gen str first=word(namefrst,1)
	replace first=subinstr(first, ".", "", .)
	replace first=trim(first)
	replace first=proper(first)
	*** middle name
	gen str middle=word(namefrst,2)
	replace middle=proper(middle)
	
	gen init = "X" if middle!=""
	
	*** obvious abbreviations (not nicknames)
	replace first="William" if first=="Wm"
	replace first="George" if first=="Geo"
	replace first="Charles" if first=="Chas"
	replace first="Daniel" if first=="Danl"
	replace first="James" if first=="Jas"
	replace first="Joseph" if first=="Jos"
	replace first="Robert" if first=="Robt"
	replace first="Richard" if first=="Richd"
	replace first="Samuel" if first=="Saml"
	replace first="Thomas" if first=="Thos"
	replace first="Frederick" if first=="Fredk"
	replace first="Frederick" if first=="Fred'K" 
	replace first="John" if first=="Jno" 
	replace first="Samuel" if first=="Sam'L"  
	replace first="Thomas" if first=="Tho" 
	replace first="Michael" if first=="Michl"

gen year1 = 1850
gen year2 = 1880

gen husb = sex==1 & relate==1 & sploc==2
gen wife = sex==2 & relate==2 & sploc==1

tab husb if sex==1
tab wife if sex==2

sort serial relate
gen husbbpl = bpl if husb==1
replace husbbpl = husbbpl[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifebpl = bpl if wife==1
replace wifebpl = wifebpl[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbrace = race if husb==1
replace husbrace = husbrace[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wiferace = race if wife==1
replace wiferace = wiferace[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbage = age if husb==1
replace husbage = husbage[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifeage = age if wife==1
replace wifeage = wifeage[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen spouseage = husbage if wife==1
replace spouseage = wifeage if husb==1
gen agegap = husbage - wifeage

gen spousebpl = husbbpl if wife==1
replace spousebpl = wifebpl if husb==1

gen spouserace = husbrace if wife==1
replace spouserace = wiferace if husb==1

gen husboccscore = occscore if husb==1
replace husboccscore = husboccscore[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen spouseregion_birth = 11 if spousebpl==9 | spousebpl==23 | spousebpl==25 | spousebpl==33 | spousebpl==44 | spousebpl==50
replace spouseregion_birth = 12 if spousebpl==36 | spousebpl==34 | spousebpl==42
replace spouseregion_birth = 21 if spousebpl==17 | spousebpl==18 | spousebpl==26 | spousebpl==39 | spousebpl==55
replace spouseregion_birth = 22 if spousebpl==19 | spousebpl==20 | spousebpl==27 | spousebpl==29 | spousebpl==31 | spousebpl==38 | spousebpl==46
replace spouseregion_birth = 31 if spousebpl== 10 | spousebpl==11 | spousebpl==24 | spousebpl==12 | spousebpl==13 | spousebpl==37 | spousebpl==45 | spousebpl==51 | spousebpl==54
replace spouseregion_birth = 32 if spousebpl==1 | spousebpl==21 | spousebpl==28 | spousebpl==47
replace spouseregion_birth = 33 if spousebpl==5 | spousebpl==22 | spousebpl==40 | spousebpl==48
replace spouseregion_birth = 41 if spousebpl==4 | spousebpl==8 | spousebpl==16 | spousebpl==32 | spousebpl==30 | spousebpl==35 | spousebpl==56 | spousebpl==49
replace spouseregion_birth = 42 if spousebpl==6 | spousebpl==41 | spousebpl==53
	
gen spouseregion_birth_coarse = floor(spouseregion_birth/10)
replace spouseregion_birth_coarse = 5 if spousebpl>=100 & spousebpl!=.

drop husb wife

keep if (age>=30 & age<=55 & sex == 1) | (age>=30 & age<=45 & sex == 2)

mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen linked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen linked_byreg = _merge==3
drop _merge

foreach var in $incvars{
	gen missing = (logocc`var'_father == .)
	sort sex missing logocc`var'_father randomorder
	by sex missing: gen qtl_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_`var'_adultdist = . if missing == 1
	drop missing
	
	gen missing = (logocc`var'_father_reg == .)
	sort sex missing logocc`var'_father_reg randomorder
	by sex missing: gen qtl_reg_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_reg_`var'_adultdist = . if missing == 1
	drop missing
}

gen married = marst==1 | marst==2
gen ever_married = marst!=6


foreach var in name_count* logocc* qtl* byname* {
	rename `var' own`var'
}

replace year1 = year1 - 10 

mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen linked_old = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen linked_old_byreg = _merge==3
drop _merge

foreach var in name_count* logocc* qtl* byname* {
	rename `var' old`var'
}

replace year1 = year1 + 10 

**** Assign husband's and wife's father's occwealth***

gen husb = sex==1 & relate==1 & sploc==2
gen wife = sex==2 & relate==2 & sploc==1

tab husb if sex==1
tab wife if sex==2

sort serial relate
gen husbfirst = first if husb==1
replace husbfirst = husbfirst[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifefirst = first if wife==1
replace wifefirst = wifefirst[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen spousefirst = husbfirst if wife==1
replace spousefirst = wifefirst if husb==1
gen spousesex = 1 if wife==1
replace spousesex = 2 if husb==1

rename sex ownsex
rename first ownfirst
rename spousefirst first
rename spousesex sex


mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen spouselinked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen spouselinked_byreg = _merge==3
drop _merge

foreach var in $incvars{
	gen missing = (logocc`var'_father == .)
	sort sex missing logocc`var'_father randomorder
	by sex missing: gen qtl_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_`var'_adultdist = . if missing == 1
	drop missing
	
	gen missing = (logocc`var'_father_reg == .)
	sort sex missing logocc`var'_father_reg randomorder
	by sex missing: gen qtl_reg_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_reg_`var'_adultdist = . if missing == 1
	drop missing
}

foreach var in name_count* logocc* qtl* byname*{
	rename `var' sp`var'
}

replace year1 = year1 - 10 

mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen spouselinked_old = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen spouselinked_old_byreg = _merge==3
drop _merge

replace year1 = year1 + 10 

rename first spousefirst
rename sex spousesex
rename ownsex sex
rename ownfirst first	

foreach var in name_count* logocc* qtl* byname*{
	rename `var' `var'_spold
	rename own`var' `var'
	rename old`var' `var'_old
	rename sp`var' `var'_spouse
}

foreach var in $incvars{
	gen rankocc`var'_father = .
	xtile x = logocc`var'_father if sex==1, nq(100)
	xtile y = logocc`var'_father if sex==2, nq(100)
	replace rankocc`var'_father = x if sex==1
	replace rankocc`var'_father = y if sex==2
	
	gen rankocc`var'_father_spouse = .
	xtile w = logocc`var'_father_spouse if sex==1, nq(100)
	xtile z = logocc`var'_father_spouse if sex==2, nq(100)
	replace rankocc`var'_father_spouse = w if sex==1
	replace rankocc`var'_father_spouse = z if sex==2

	drop w x y z
}

save $tempdir/panel_trends_30yr_exage.dta, replace

foreach y in $yearlistshort {

	use $rawdir/`y'_1%.dta, clear
	
	set seed 828
	gen randomorder = runiform()
	
	/*
	if `y'==1940 {
	
		drop if serial==""
		
		rename serial serial_string
		egen serial = group(serial_string)
		drop serial_string
		rename statefips statefip
		rename hhorder pernum
		
		gen region = 11 if statefip==9 | statefip==23 | statefip==25 | statefip==33 | statefip==44 | statefip==50
		replace region = 12 if statefip==36 | statefip==34 | statefip==42
		replace region = 21 if statefip==17 | statefip==18 | statefip==26 | statefip==39 | statefip==55
		replace region = 22 if statefip==19 | statefip==20 | statefip==27 | statefip==29 | statefip==31 | statefip==38 | statefip==46
		replace region = 31 if statefip== 10 | statefip==11 | statefip==24 | statefip==12 | statefip==13 | statefip==37 | statefip==45 | statefip==51 | statefip==54
		replace region = 32 if statefip==1 | statefip==21 | statefip==28 | statefip==47
		replace region = 33 if statefip==5 | statefip==22 | statefip==40 | statefip==48
		replace region = 41 if statefip==4 | statefip==8 | statefip==16 | statefip==32 | statefip==30 | statefip==35 | statefip==56 | statefip==49
		replace region = 42 if statefip==6 | statefip==41 | statefip==53
		
		rename bpl bpl_orig
		
		mer m:1 bpl_orig using $rawdir/1940_bpls.dta, keepus(bpl_string bpl)
		drop _merge
		
		replace bpl = statefip if bpl==0
		
		do $rawdir/labels_ipums.do
		
		label values statefip bpl_lbl
		
		sort serial pernum
		gen sploc = 2 if sex==1 & relate==1 & marst==1 & sex[_n+1]==2 & relate[_n+1]==2 & marst[_n+1]==1
		replace sploc = 1 if sex==2 & relate==2 & marst==1 & sex[_n-1]==1 & relate[_n-1]==1 & marst[_n-1]==1
		replace sploc = 0 if sploc==.
		
		keep namefrst sex age marst sploc relate statefip region bpl serial pernum race occscore randomorder
		
	}	
	*/

	gen region_birth = 11 if bpl==9 | bpl==23 | bpl==25 | bpl==33 | bpl==44 | bpl==50
	replace region_birth = 12 if bpl==36 | bpl==34 | bpl==42
	replace region_birth = 21 if bpl==17 | bpl==18 | bpl==26 | bpl==39 | bpl==55
	replace region_birth = 22 if bpl==19 | bpl==20 | bpl==27 | bpl==29 | bpl==31 | bpl==38 | bpl==46
	replace region_birth = 31 if bpl== 10 | bpl==11 | bpl==24 | bpl==12 | bpl==13 | bpl==37 | bpl==45 | bpl==51 | bpl==54
	replace region_birth = 32 if bpl==1 | bpl==21 | bpl==28 | bpl==47
	replace region_birth = 33 if bpl==5 | bpl==22 | bpl==40 | bpl==48
	replace region_birth = 41 if bpl==4 | bpl==8 | bpl==16 | bpl==32 | bpl==30 | bpl==35 | bpl==56 | bpl==49
	replace region_birth = 42 if bpl==6 | bpl==41 | bpl==53
	
	gen region_birth_coarse = floor(region_birth/10)
	replace region_birth_coarse = 5 if bpl>=100 & bpl!=.
	
	*** NAMES
	*** first name 
	gen str first=word(namefrst,1)
	replace first=subinstr(first, ".", "", .)
	replace first=trim(first)
	replace first=proper(first)
	*** middle name
	gen str middle=word(namefrst,2)
	replace middle=proper(middle)
	
	gen init = "X" if middle!=""
	
	*** obvious abbreviations (not nicknames)
	replace first="William" if first=="Wm"
	replace first="George" if first=="Geo"
	replace first="Charles" if first=="Chas"
	replace first="Daniel" if first=="Danl"
	replace first="James" if first=="Jas"
	replace first="Joseph" if first=="Jos"
	replace first="Robert" if first=="Robt"
	replace first="Richard" if first=="Richd"
	replace first="Samuel" if first=="Saml"
	replace first="Thomas" if first=="Thos"
	replace first="Frederick" if first=="Fredk"
	replace first="Frederick" if first=="Fred'K" 
	replace first="John" if first=="Jno" 
	replace first="Samuel" if first=="Sam'L"  
	replace first="Thomas" if first=="Tho" 
	replace first="Michael" if first=="Michl"

gen year1 = `y'-30
gen year2 = `y'

gen husb = sex==1 & relate==1 & sploc==2
gen wife = sex==2 & relate==2 & sploc==1

tab husb if sex==1
tab wife if sex==2

sort serial relate
gen husbbpl = bpl if husb==1
replace husbbpl = husbbpl[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifebpl = bpl if wife==1
replace wifebpl = wifebpl[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbrace = race if husb==1
replace husbrace = husbrace[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wiferace = race if wife==1
replace wiferace = wiferace[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen husbage = age if husb==1
replace husbage = husbage[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifeage = age if wife==1
replace wifeage = wifeage[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen spouseage = husbage if wife==1
replace spouseage = wifeage if husb==1
gen agegap = husbage - wifeage

gen spousebpl = husbbpl if wife==1
replace spousebpl = wifebpl if husb==1

gen spouserace = husbrace if wife==1
replace spouserace = wiferace if husb==1

gen husboccscore = occscore if husb==1
replace husboccscore = husboccscore[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen spouseregion_birth = 11 if spousebpl==9 | spousebpl==23 | spousebpl==25 | spousebpl==33 | spousebpl==44 | spousebpl==50
replace spouseregion_birth = 12 if spousebpl==36 | spousebpl==34 | spousebpl==42
replace spouseregion_birth = 21 if spousebpl==17 | spousebpl==18 | spousebpl==26 | spousebpl==39 | spousebpl==55
replace spouseregion_birth = 22 if spousebpl==19 | spousebpl==20 | spousebpl==27 | spousebpl==29 | spousebpl==31 | spousebpl==38 | spousebpl==46
replace spouseregion_birth = 31 if spousebpl== 10 | spousebpl==11 | spousebpl==24 | spousebpl==12 | spousebpl==13 | spousebpl==37 | spousebpl==45 | spousebpl==51 | spousebpl==54
replace spouseregion_birth = 32 if spousebpl==1 | spousebpl==21 | spousebpl==28 | spousebpl==47
replace spouseregion_birth = 33 if spousebpl==5 | spousebpl==22 | spousebpl==40 | spousebpl==48
replace spouseregion_birth = 41 if spousebpl==4 | spousebpl==8 | spousebpl==16 | spousebpl==32 | spousebpl==30 | spousebpl==35 | spousebpl==56 | spousebpl==49
replace spouseregion_birth = 42 if spousebpl==6 | spousebpl==41 | spousebpl==53
	
gen spouseregion_birth_coarse = floor(spouseregion_birth/10)
replace spouseregion_birth_coarse = 5 if spousebpl>=100 & spousebpl!=.

drop husb wife

keep if (age>=30 & age<=55 & sex == 1) | (age>=30 & age<=45 & sex == 2)

mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen linked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen linked_byreg = _merge==3
drop _merge

foreach var in $incvars{
	gen missing = (logocc`var'_father == .)
	sort sex missing logocc`var'_father randomorder
	by sex missing: gen qtl_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_`var'_adultdist = . if missing == 1
	drop missing
	
	gen missing = (logocc`var'_father_reg == .)
	sort sex missing logocc`var'_father_reg randomorder
	by sex missing: gen qtl_reg_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_reg_`var'_adultdist = . if missing == 1
	drop missing
}

gen married = marst==1 | marst==2
gen ever_married = marst!=6

foreach var of varlist name_count* logocc* qtl* byname* {
	rename `var' own`var'
}

replace year1 = year1 - 10 

mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen linked_old = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen linked_old_byreg = _merge==3
drop _merge

foreach var of varlist name_count* logocc* qtl* byname* {
	rename `var' old`var'
}

replace year1 = year1 + 10 

**** Assign husband's and wife's father's occwealth ***

gen husb = sex==1 & relate==1 & sploc==2
gen wife = sex==2 & relate==2 & sploc==1

tab husb if sex==1
tab wife if sex==2

sort serial relate
gen husbfirst = first if husb==1
replace husbfirst = husbfirst[_n-1] if serial==serial[_n-1] & wife==1 & husb[_n-1]==1

gen wifefirst = first if wife==1
replace wifefirst = wifefirst[_n+1] if serial==serial[_n+1] & husb==1 & wife[_n+1]==1

gen spousefirst = husbfirst if wife==1
replace spousefirst = wifefirst if husb==1
gen spousesex = 1 if wife==1
replace spousesex = 2 if husb==1

rename sex ownsex
rename first ownfirst
rename spousefirst first
rename spousesex sex


mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen spouselinked = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen spouselinked_byreg = _merge==3
drop _merge

foreach var in $incvars{
	gen missing = (logocc`var'_father == .)
	sort sex missing logocc`var'_father randomorder
	by sex missing: gen qtl_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_`var'_adultdist = . if missing == 1
	drop missing
	
	gen missing = (logocc`var'_father_reg == .)
	sort sex missing logocc`var'_father_reg randomorder
	by sex missing: gen qtl_reg_`var'_adultdist = ceil((_n / _N) / 0.25)
	replace qtl_reg_`var'_adultdist = . if missing == 1
	drop missing
}

foreach var of varlist name_count* logocc* qtl* byname*{
	rename `var' sp`var'
}

replace year1 = year1 - 10 

mer m:1 year1 sex first using $tempdir/childnames_income.dta, keepus(name_count* logocc* qtl* byname_*)
drop if _merge==2
gen spouselinked_old = _merge==3
drop _merge

mer m:1 year1 sex first region_birth_coarse using $tempdir/childnames_byregion_income.dta, keepus(name_count* logocc* qtl* bynamereg_*)
drop if _merge==2
gen spouselinked_old_byreg = _merge==3
drop _merge

replace year1 = year1 + 10 

rename first spousefirst
rename sex spousesex
rename ownsex sex
rename ownfirst first	

foreach var of varlist name_count* logocc* qtl* byname*{
	rename `var' `var'_spold
	rename own`var' `var'
	rename old`var' `var'_old
	rename sp`var' `var'_spouse
}

	
foreach var in $incvars{
	gen rankocc`var'_father = .
	xtile x = logocc`var'_father if sex==1, nq(100)
	xtile y = logocc`var'_father if sex==2, nq(100)
	replace rankocc`var'_father = x if sex==1
	replace rankocc`var'_father = y if sex==2
	
	gen rankocc`var'_father_spouse = .
	xtile w = logocc`var'_father_spouse if sex==1, nq(100)
	xtile z = logocc`var'_father_spouse if sex==2, nq(100)
	replace rankocc`var'_father_spouse = w if sex==1
	replace rankocc`var'_father_spouse = z if sex==2

	drop w x y z
}
	
	append using $tempdir/panel_trends_30yr_exage.dta
	save $tempdir/panel_trends_30yr_exage.dta, replace
	
}

